package com.xzx.simple;

/**
 * @className com.xzx.simple algorithm
 * @description:回文数
 * @author: xzx
 * @date: 19-3-25 14:52
 * @version: v1
 */
public class PalindromeNumber {

    /**
     * 判断一个整数是否是回文数。回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。例如：121
     * @param x
     * @return
     */
    public boolean isPalindrome(int x) {
        return false;
    }

    /**
     * 232 ms	100.1 MB
     * @param x
     * @return
     */
    public boolean isPalindrome1(int x) {
        String o = String.valueOf(x);
        StringBuilder builder = new StringBuilder(o);
        builder.reverse();
        String n = builder.toString();
        if (o.equals(n)){
            return true;
        }
        return false;
    }

    /**
     * 233 ms	97.7 MB
     * @param x
     * @return
     */
    public boolean isPalindrome2(int x) {
        if (x < 0){
            return false;
        }
        int x1 = x;
        int re = 0;
        while (x != 0){
            int pop = x % 10 ;
            x = x / 10;
            re = re * 10 + pop;
        }
        return re == x1 ? true : false;
    }

    /**
     * 225 ms	96.8 MB
     * @param x
     * @return
     */
    public boolean isPalindrome3(int x) {
        if(x < 0 || (x % 10 == 0 && x != 0)) {
            return false;
        }

        int revertedNumber = 0;
        while(x > revertedNumber) {
            revertedNumber = revertedNumber * 10 + x % 10;
            x /= 10;
        }

        return x == revertedNumber || x == revertedNumber/10;
    }
}
